package com.example.asus.homework11.activity;

import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;


import com.example.asus.homework11.R;
import com.example.asus.homework11.RecyclerViewAdapter;
import com.example.asus.homework11.bean.TestItem;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    public void initView() {
        //定义适配器对象
        RecyclerViewAdapter adapter = new RecyclerViewAdapter();
        //获取数据
        adapter.setData(getData());
        //需将布局中的RecyclerView对象的id指定为recyclerview
        recyclerView = findViewById(R.id.recyclerView);
        //定义线性⽅式显示数据
        LinearLayoutManager layoutManager =
                new LinearLayoutManager(MainActivity.this);
        //设置为位垂直⽅式显示（也可以⽔平⽅式显示）
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        //设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
        //设置适配器
        recyclerView.setAdapter(adapter);
        //以下代码为添加分割线（需⾃⼰完成）
        recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));

    }
    // 获取数据集合
    private List<TestItem> getData(){
    List<TestItem> data = new ArrayList<>();
        for (int i = 0; i < 10; i ++){
            TestItem dataItem = new TestItem();
            dataItem.setUrl("image" + i);
            dataItem.setText1("text1:" + i);
            dataItem.setText2("text2:" + i);

        data.add(dataItem);
        }
        return data;
    }

}
